<?php
namespace app\overlord\model;

use app\BaseModel;
use think\facade\Db;

class Order extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_overlord_order';

    protected $append = [

        'user_name'
    ];


    /**
     * @author chenniang
     * @DataTime: 2020-11-30 11:54
     * @功能说明:操作人名称
     */
    public function getUserNameAttr($value,$data){

        if(!empty($data['user_id'])){

            return lbUserName($data['user_id']);
        }

    }





    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:内容添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page){

        $data = $this->alias('a')
                ->join('longbing_card_overlord_atv b','a.atv_id = b.id')
                ->where($dis)
                ->field('a.*,b.title as atv_title,b.num')
                ->group('a.id')
                ->order('a.id desc')
                ->paginate($page)
                ->toArray();

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $v['user_name'] = lbUserName($v['user_id']);

            }
        }

        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-26 13:45
     * @功能说明:支付回调
     */
    public function orderResult($order_code,$transaction_id){

        $order = $this->dataInfo(['order_code'=>$order_code,'pay_type'=>0]);

        if(!empty($order)){

            $update= [

                'pay_time' => time(),

                'pay_type' => 1,

                'transaction_id' => $transaction_id
            ];

            $this->where(['id'=>$order['id']])->update($update);

            $atv_model = new Active();

            $atv_model->where(['id'=>$order['atv_id']])->update(['status'=>1]);

        }

        return true;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-26 17:38
     * @功能说明:获取门店总充值金额
     */

    public function getStoreFee($store_id){

        $dis = [

            'store_id' => $store_id,

            'pay_type' => 1
        ];

        $fee = $this->where($dis)->sum('pay_price');

        return round($fee,2);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-30 09:58
     * @功能说明:支付退款记录
     */
    public function orderFrom($dis,$where,$page){

        $a = Db::name('longbing_card_overlord_refund_order')->field(['id','status','refund_price as price','create_time','null as transaction_id'])->where($where)->order('create_time desc,id desc')->buildSql();

        $page = intval($page);

        $data = $this->where($dis)->field(['id','pay_type as status','pay_price as price','create_time','transaction_id'])->unionAll([$a])->order('create_time desc,id desc')->paginate(10,$page)->toArray();

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
            }
        }

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-30 11:44
     * @功能说明:后台订单列表
     */
    public function adminOrderList($store_id,$page=10,$title = ''){

        $dis[] = ['a.store_id','=',$store_id];

        $dis[] = ['a.pay_type','=',1];

        if(!empty($title)){

            $dis[] = ['b.title','like','%'.$title.'%'];
        }

        $data = $this->alias('a')
                ->join('longbing_card_overlord_atv b','a.atv_id = b.id','left')
                ->where($dis)
                ->field('b.title,b.num,b.cash,a.pay_type,a.pay_price as total_cash,a.create_time,a.id')
                ->group('a.id')
                ->order('a.id desc')
                ->paginate($page)
                ->toArray();

        return $data;

    }












}